﻿//This file is part of ILoveHUSTBoard.

//ILoveHUSTBoard is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//ILoveHUSTBoard is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with ILoveHUSTBoard.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;

namespace WebSite.actions
{
    public partial class add_message : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string content = "";
            string nickname = "";

            bool isInputValid = true;
            try
            {
                if (string.IsNullOrEmpty((string)Request["message"]) || Session["login"] == null || ((string)Request["message"]).Length>300)
                    throw new ArgumentOutOfRangeException("request_values","传入参数非法");

                content = Request["message"];
                content = content.Replace("\r\n", "<br />");//处理分段
                nickname = Session["login"].ToString();
            }
            catch
            {
                isInputValid=false;
            }

            is_user_loggedin login = new is_user_loggedin();
       

            if(isInputValid&&login.IsUserLoggedIn())
            {
                DataHandler.message.HarmonyProcessor harmony = new DataHandler.message.HarmonyProcessor();
                if (!harmony.IsContentHarmoniousEnough(content, ConfigurationManager.AppSettings["hexie_wordlist"]))
                    Response.Redirect("/index.aspx?error=hexie");

                DataHandler.message.MessageProcessor messageProcessor = new DataHandler.message.MessageProcessor();
                if (messageProcessor.AddMessage(new DataHandler.views.MessageInfo() { MessageContent = content, NickName = nickname, SubmitTime = DateTime.Now }))
                    Response.Write("success");
                else
                    Response.Write("fail");
            }
            else
            {
                Response.Write("error");
                if(Session["login"]==null)
                    Response.Redirect("/index.aspx?error=login");
                else
                    Response.Redirect("/index.aspx?error=unknown");
            }
            Response.Redirect("/index.aspx");
        }
    }
}